(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.finance.previewVerifyController', createPreviewVerifyController)
	.filter("frReqStatus", [ '$filter', '$locale', 'discountService', function($filter, $locale, discountService) {
		return function(frReqStatus, pattern) {
			return $filter('translate')(discountService.frReqStatusFilter(frReqStatus, pattern));
		};
	} ]);
	createPreviewVerifyController.$inject = [ '$scope', 'Constants', 'formatService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService','storageService',
			'piDiscountService', 'discountService','arithService' ];
	function createPreviewVerifyController($scope, Constants, formatService, modalService, gridService, layoutConfig, validatorConfig, authService,storageService, piDiscountService, discountService,arithService) {
		var ctrl = this, piDiscountInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piDiscountRequest = {};
		
		discountService.getDiscountListOptions();
		
		ctrl.init = function() {
			ctrl.loanGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.loanTable, null);
			ctrl.loanGridOptions.data = 'vm.piDiscountRequest.assetLoanList';
			ctrl.loanGridOptions.enableRowHeaderSelection = false;
			ctrl.invoiceGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.invoiceTable, null);
			ctrl.invoiceGridOptions.data = 'vm.piDiscountRequest.discountInvoiceList';
			ctrl.invoiceGridOptions.enableRowHeaderSelection = false;
			authService.createPreviewVerify(piDiscountInfo).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					var piDiscountRequest = response.data.model;
					var totalAmt = parseFloat(arithService.add(piDiscountRequest.intTotalAmt, piDiscountRequest.feeTotalAmt));
					if(arithService.eq(piDiscountRequest.loanTotalAmt, 0)  ){
						piDiscountRequest.costCelling = 0;
					}else{
						piDiscountRequest.costCelling = parseFloat(arithService.mul(arithService.div(totalAmt,piDiscountRequest.loanTotalAmt),100)).toFixed(2);
					}
					angular.forEach(piDiscountRequest.discountInvoiceList, function(data) {
						if(data.communityCodeQ != null && data.communityCode != null){
							data.community = data.communityCodeQ + ":" + data.communityCode;
						}
					});
					
					ctrl.piDiscountRequest = piDiscountRequest;
					ctrl.piDiscountRequest.taskId = response.data.task.taskId;
				}
				
			});
		};

		ctrl.createPreviewVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.piDiscountRequest.message = comments;
						authService.createPreviewVerifyReject(ctrl.piDiscountRequest).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};
		
		
		ctrl.viewInvoiceDetails = function(rowEntity){
			var params = {};
			params.invoice = rowEntity;
			modalService.openCustomDialogs({
				'title': 'title.invoice.detail',
				'dialogsModule' : 'apf.invoice.detail',
				'params' : params,
				'btnCallback' : {
					'ok' : function() {
						
					}
				}
			});
		};
		ctrl.openDisCountDetail = function(value) {
			modalService.openCustomDialogs({
				'dialogsModule' : 'apf.disccharge.detail',
				'params' : {
					"disCountDetail" : value,
					"loanCcy" : ctrl.piDiscountRequest.loanCcy
				}
			});
		};

		ctrl.createPreviewVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.piDiscountRequest.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createPreviewVerifyApprove(ctrl.piDiscountRequest).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.pidiscount.create',
					'state' : 'apf.finance.create',
					'params' : {}
				}]
			});
		}
		
		
		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.pidiscount.create',
					'state' : 'apf.finance.create',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		
		
	}
})(angular);